package de.koller.zero;

import de.koller.oceanFramework.OceanException;
import de.koller.zero.arch.ZNode;
import de.koller.zero.engine.ZeroEngine;
import de.koller.zero.engine.commands.StopNode;
import de.koller.zero.model.Realm;
import de.koller.zero.view.ZeroGUI;
import java.util.Random;
import javax.swing.UIManager;

public class ZeroRoot extends ZNode {
	
	public static final Random RND = new Random();
	public static final String TITLE = "200 Million Z v0.0";
	
	public final ZeroEngine engine = new ZeroEngine();
	public final Realm realm = new Realm();
	public final ZeroGUI gui = new ZeroGUI();

	@Override
	public void generate( ZeroRoot root, Object... references ) {
		super.generate( root, references );
		
		this.addNode( engine );
		this.addNode( realm );
		this.addNode( gui );
		
		gui.updateNode();
	}

	@Override
	public void ungenerate() {
		super.ungenerate();
		
		System.out.println( "Beendet!" );
	}
	
	public void start() {
		this.generate( this );
		engine.start();
		gui.start();
		
		try {
			engine.getThread().join();
			gui.getThread().join();
		} catch( InterruptedException ex ) {
			throw new OceanException( "interrupted!" );
		}
		
		this.ungenerate();
	}
	
	public void exit() {
		engine.call( new StopNode() );
		gui.call( new StopNode() );
	}
	
	public static void main( String[] args ) {
		try {
//			UIManager.setLookAndFeel( "javax.swing.plaf.nimbus.NimbusLookAndFeel" );
//			UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel" );
//			UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
		} catch( Exception e ) {
			System.out.println("Error setting native LAF: " + e);
		}
		
		ZeroRoot zero = new ZeroRoot();
		zero.start();
	}
	
}
